﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ModifyClient : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int clientId = int.Parse(Request.QueryString["ClientId"]);
                NxClient nxClient = ServiceLocator.IClientService.GetNxClientById(clientId);
                this.txtCellPhone.Text = nxClient.Cellphone;
                this.txtCompany.Text = nxClient.Company;
                this.txtUserName.Text = nxClient.UserName;
                this.hidOldCellPhone.Value = nxClient.Cellphone;
                if (nxClient.Sex == 1)
                    this.rblSex.Items[0].Selected = true;
                else
                    this.rblSex.Items[1].Selected = true;
                this.txtAge.Text = nxClient.Age.ToString();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int clientId = int.Parse(Request.QueryString["ClientId"]);
            NxClient client = ServiceLocator.IClientService.GetNxClientById(clientId);
            if (txtCellPhone.Text.Trim() != "" && txtCompany.Text.Trim() != "")
            {
                client.ClientId = clientId;
                client.Cellphone = this.txtCellPhone.Text.Trim();
                client.UserName = this.txtUserName.Text.Trim();
                client.Company = this.txtCompany.Text.Trim();
                client.Sex = int.Parse(this.rblSex.SelectedValue.ToString());
                client.Age = int.Parse(this.txtAge.Text.Trim());

                if (!ServiceLocator.IClientService.UpdateNxClient(client))
                {
                    JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "SAVE_ERROR") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                }
                else
                {
                    JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "SAVE_SUCCESSFUL") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "','ClientList.aspx')", this.Page);
                }
            }
            else
            {
                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "NUMBER_IS_REQUIRED") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
            }   
        }
    }
}
